//
//  YTPhotoResourceController.m
//  YUNT
//
//  Created by haha on 2017/11/26.
//  Copyright © 2017年 梁炜东. All rights reserved.
//

#import "YTPhotoResourceController.h"
#import "YTPhotoLibraryController.h"
#import "YTPhotoLibraryManager.h"

@interface YTPhotoResourceController ()

@property (nonatomic, assign) BOOL isSelect;

@property (nonatomic, strong) YTPhotoLibraryManager *manager;

@property (nonatomic, strong) UIView *bottomView;

@end

@implementation YTPhotoResourceController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    
    self.title = Language(@"图库");
    
    self.manager = [YTPhotoLibraryManager defaultManager];
    
    [self.manager readResourcesFromSandbox];
    
    YTPhotoLibraryController *childAll = [[YTPhotoLibraryController alloc] init];
    
    childAll.title = Language(@"全部");
    
    childAll.allKeys = self.manager.allKeys;
    
    childAll.resources = self.manager.allResources;
    
    childAll.resourceType = YTResouceTypeAll;
    
    YTPhotoLibraryController *childVideo = [[YTPhotoLibraryController alloc] init];
    
    childVideo.title = Language(@"视频");
    
    childVideo.allKeys = self.manager.videoKeys;
    
    childVideo.resources = self.manager.allVideos;
    
    childVideo.resourceType = YTResouceTypeVideo;
    
    YTPhotoLibraryController *childImage = [[YTPhotoLibraryController alloc] init];
    
    childImage.title = Language(@"图片");
    
    childImage.allKeys = self.manager.imageKeys;
    
    childImage.resources = self.manager.allImages;
    
    childImage.resourceType = YTResouceTypeImage;
    
    self.viewControllers = @[childAll,childVideo,childImage];
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"rf_back"] style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
    
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"选择" style:UIBarButtonItemStylePlain target:self action:@selector(selectResources)];
    
    rightItem.tintColor = [UIColor colorWithRed:(0.0f) green:(161.0f / 255.0f) blue:(244.0f / 255.0f) alpha:1.0f];
    
    self.navigationItem.rightBarButtonItem = rightItem;
    
    [self initBottomView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initBottomView
{
    self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.height, self.view.width, 40)];
    
    self.bottomView.backgroundColor = [UIColor colorWithRed:74/255.0 green:74/255.0 blue:74/255.0 alpha:1.0];
    
    [self.view addSubview:self.bottomView];
    
    UIButton *saveButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.view.width/2-1, 40)];
    
    [saveButton setTitle:@"保存" forState:UIControlStateNormal];
    
    [saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [saveButton addTarget:self action:@selector(saveSelectedResources) forControlEvents:UIControlEventTouchUpInside];
    
    [self.bottomView addSubview:saveButton];
    
    UIView *whiteSpace = [[UIView alloc] initWithFrame:CGRectMake(self.view.width/2-1, 4, 0.5, 32)];
    
    whiteSpace.backgroundColor = [UIColor whiteColor];
    
    
    [self.bottomView addSubview:whiteSpace];
    
    UIButton *shareButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.width/2, 0, self.view.width/2-1, 40)];
    
    [shareButton setTitle:@"分享" forState:UIControlStateNormal];
    
    [shareButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [shareButton addTarget:self action:@selector(shareSelectedResources) forControlEvents:UIControlEventTouchUpInside];
    
    [self.bottomView addSubview:shareButton];
}

#pragma Responder

- (void)goBack
{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

- (void)selectResources
{
    self.isSelect = !self.isSelect;

    [(YTPhotoLibraryController *)self.selectedViewController updateSelectStatus:self.isSelect];
    
    if (self.isSelect)
    {
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(deleteSelectedResources)];
        
        self.navigationItem.leftBarButtonItem.enabled = NO;
        
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(selectResources)];
        
        [UIView animateWithDuration:0.3 animations:^{
            self.bottomView.originY -= 40;
        }];
    }
    else
    {
        [[YTPhotoLibraryManager defaultManager].selectedResources removeAllObjects];
        
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"rf_back"] style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
        
        self.navigationItem.leftBarButtonItem.enabled = YES;
        
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"选择" style:UIBarButtonItemStylePlain target:self action:@selector(selectResources)];
        
        [UIView animateWithDuration:0.3 animations:^{
            self.bottomView.originY += 40;
        }];
    }
}


- (void)deleteSelectedResources
{
    for (YTPhotoLibraryItem *item in [YTPhotoLibraryManager defaultManager].selectedResources)
    {
        NSError *error = nil;
        
        NSString *str = [kPhotoPath stringByAppendingString:item.resourceName];
        
        [FileManager removeItemAtURL:[NSURL fileURLWithPath:str] error:&error];
    }
    //TODO:REFRESH
}

- (void)saveSelectedResources
{
    
}

- (void)shareSelectedResources
{
    
}

- (void)segementViewController:(XYSegementViewController *)viewController didSelectRowAtIndexPath:(NSInteger)indexPath
{
    [super segementViewController:viewController didSelectRowAtIndexPath:indexPath];
    
    [(YTPhotoLibraryController *)self.selectedViewController updateSelectStatus:self.isSelect];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
